package com.easy.common.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.easy.common.constant.Const;

public class Utility {

	private static Logger log = null;
	public static final int WITHGROUP = 0;
	public static final int WITHOUTGROUP = 1;
	public static String DATE_PATTERN = "yyyy-MM-dd";
	public static String DATE_PATTEN_SECOND = "yyyy-MM-dd HH:mm:ss";
	public static long DATE_DAY = 86400000L;
	public static long DATE_HOUR = 3600000L;
	public static long DATE_MINIT = 60000L;

	private static DecimalFormat numFormat = new DecimalFormat("#0.00");
	private static DecimalFormat numFormatG = new DecimalFormat("#,##0.00");
	private static SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static SimpleDateFormat dateFormatS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
	public static String DATE_FORMAT_NOSPLIT = "yyyyMMddHHmmssSSS";
	private static SimpleDateFormat dateFormatEn;
	private static SimpleDateFormat dateFormatCn;
	private static SimpleDateFormat dateFormatYMEn;
	private static SimpleDateFormat dateFormatYMCn;
	private static SimpleDateFormat dateFormatMDEn;
	private static SimpleDateFormat dateFormatMDCn;
	public static final int decimalScale = 2;
	private static final String enSmallNumber[] = {
		"", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", 
		"TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN"
	};
	private static final String enLargeNumber[] = {
		"TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY"
	};
	private static final String enUnit[] = {
		"", "THOUSAND", "MILLION", "BILLION", "TRILLION"
	};
	private static final Class<?> MYUtility = null;

	public Utility()
	{
	}
	/**
	 * 判断str是否为NULL值或者为空
	 * @param str 字段串
	 * @return 为NULL值或者为空时返加true  不为空时返回false
	 */
	public static boolean isNullorEmpty(String str)
	{
		if (str == null)return true;
		str = str.trim();
		return str.length() < 1;
	}
	/**
	 * 判断str是否为NULL，为NULL值时返为空串，否则返为str
	 * @param str 字符串
	 * @return 为NULL时返为空串，　否则返为str
	 */
	public static String null2Empty(String str)
	{
		if (str == null)
			return "";
		else
			return str;
	}

	/**
	 * 判断str是否为NULL，为NULL值时返为空串，否则返为str
	 * @param str 字符串
	 * @return 为NULL时返为0.00，　否则返为str
	 */
	public static String null2Zero(String val){
		if(val==null || val.equals(""))return "0.00";
		else return val;
	}

	/**
	 * 判断str是否为NULL或者空串．
	 * @param str 输入字符串
	 * @param defaultStr　
	 * @return　　如果str是NULL或者空串返回defaultStr 否则返回 str
	 */
	public static String empty2Default(String str, String defaultStr)
	{
		if (isNullorEmpty(str))
			return defaultStr;
		else
			return str;
	}

	/**
	 * 将Decimal定点数转换为字符串
	 * @param num　定点数 Decimal
	 * @param pattern 0为千分位格式分隔如1,322.01　　1常规格式如 1322.01 
	 * @return 返回指定格式字符串
	 */
	public static String getFormatedStr(BigDecimal num, int pattern)
	{
		if(num==null)return "0.00";
		if (pattern == 1)
			return numFormat.format(num);
		else
			return numFormatG.format(num);
	}

	/**
	 * 将double浮点数转换为字符串
	 * @param num　浮点数 double
	 * @param pattern 0为千分位格式分隔如1,322.01　　1常规格式如 1322.01 
	 * @return 返回指定格式字符串
	 */
	public static String getFormatedStr(double num, int pattern)
	{
		if (pattern == 1)
			return numFormat.format(num);
		else
			return numFormatG.format(num);
	}

	/**
	 * 将字符串数据转换为指定数据格式的字符串
	 * @param num　浮点数 double
	 * @param pattern 0为千分位格式分隔如1,322.01　　1常规格式如 1322.01 
	 * @return 返回指定格式字符串
	 */
	public static String getFormatedStr(String numStr, int pattern)
	{
		String rtStr = "0.00";
		try
		{
			double dValue = parseDouble(numStr);
			if (pattern == 1)
				rtStr = numFormat.format(dValue);
			else
				rtStr = numFormatG.format(dValue);
		}
		catch (Exception exception) { }
		return rtStr;
	}

	/**
	 * 取的当前时间的字符串．
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String getCurDateTimeStr()
	{
		return dateFormat.format(new Date());
	}

	/**
	 * 取的当前日期的字符串．
	 * @return yyyy-MM-dd
	 */
	public static String getCurDateStr()
	{
		return dFormat.format(new Date());
	}

	/**
	 * 按指定格式　取的当前时间的字符串
	 * @param pattern yyyy-MM-dd HH:mm:ss
	 * @return 按pattern的格式返回
	 */
	public static String getCurDateTimeStr(String pattern)
	{
		if (pattern == null)
		{
			return getCurDateTimeStr();
		} else
		{
			SimpleDateFormat format = new SimpleDateFormat(pattern);
			return format.format(new Date());
		}
	}

	/**
	 * 按指定格式　取的当前时间的字符串
	 * @param format 格式见java.text.SimpleDateFormat
	 * @return
	 */
	public static String getCurDateTimeStr(SimpleDateFormat format)
	{
		if (format == null)
			return getCurDateTimeStr();
		else
			return format.format(new Date());
	}

	/**
	 * 将日期date转换为指定格式的字符串．
	 * @param date 日期时间变量
	 * @param pattern 格式 yyyy-MM-dd HH:mm:ss
	 * @return　pattern指定的格式日期字符串
	 */
	public static String getDateTimeStr(Date date,String pattern)
	{
		if(pattern==null){
			if (date == null)
				return getCurDateTimeStr();
			else
				return dateFormat.format(date);
		}
		else {
			if (date == null)
				return getCurDateTimeStr(pattern);
			else {
				SimpleDateFormat format = new SimpleDateFormat(pattern);
				return format.format(date);
			}
		}
		
	}
	
	/**
	 * 将日期date转换为星期
	 * @param date 日期时间变量
	 * @return　0 1 2 3 4 5 6 星期日 星期一 星期二　星期三　星期四　星期五　星期六 
	 */
	public static int getDateWeek(Date date)
	{
		Calendar calendar = Calendar.getInstance();
		if(date != null)calendar.setTime(date);      
	    int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;      
	    if (w < 0)w = 0;      
	    return w;
	}
	/**
	 * 将日期date转换为星期
	 * @param date 日期时间变量
	 * @return　星期日  星期一 星期二　星期三　星期四　星期五　星期六
	 */
	public static String getDateWeekStr(Date date)
	{
		String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; 
		Calendar calendar = Calendar.getInstance();
		if(date != null)calendar.setTime(date);      
	    int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;      
	    if (w < 0)w = 0;      
	    return weekOfDays[w];
	}
	
	/**
	 * 将日期date转换为星期
	 * @param date 日期时间变量
	 * @return　0 1 2 3 4 5 6 星期日 星期一 星期二　星期三　星期四　星期五　星期六 
	 */
	public static int getCurWeek(Date date)
	{
		Calendar calendar = Calendar.getInstance();
	    int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;      
	    if (w < 0)w = 0;      
	    return w;
	}
	/**
	 * 将日期date转换为星期
	 * @param date 日期时间变量
	 * @return　星期日  星期一 星期二　星期三　星期四　星期五　星期六
	 */
	public static String getCurWeekStr(Date date)
	{
		String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; 
		Calendar calendar = Calendar.getInstance();
	    int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;      
	    if (w < 0)w = 0;      
	    return weekOfDays[w];
	}

	
	/**
	 *将BigDecimal转化为金额字符串 
	 * @param num BigDecimal类型变量
	 * @return 金额格式字符串
	 */
	public static String getAmountStr(BigDecimal num){
		if(num==null)return "0.00";
		else {
			num=num.setScale(2,BigDecimal.ROUND_HALF_UP);
			return num.toString();
		}
	}

	/**
	 * 将日期date转换为 yyyy-MM-dd HH:mm:ss 格式的字符串
	 * @param date 日期时间变量
	 * @return yyyy-MM-dd HH:mm:ss格式的日期字符串
	 */
	public static String getDateTimeStr(Date date)
	{
		if (date == null)
			return getCurDateTimeStr();
		else
			return dateFormat.format(date);
	}
	/**
	 * 
	 * @param adate
	 * @param ddate
	 */
	@SuppressWarnings("deprecation")
	public static int getMonthDiff(Date adate,Date ddate)
	{
		int mon=(ddate.getYear()-adate.getYear())*12+ddate.getMonth()-adate.getMonth();
		return mon;
	}

	/**
	 * 将日期date转换为 yyyy-MM-dd格式的字符串
	 * @param date 日期时间变量
	 * @return yyyy-MM-dd格式的日期字符串
	 */

	public static String getDateStr(Date date)
	{
		if (date == null)
			return getCurDateStr();
		else
			return dFormat.format(date);
	}

	/**
	 * 将日期date转换为 dd-MM-yyyy 英文格式的字符串
	 * @param date 日期时间变量
	 * @return dd-MM-yyyy格式的日期字符串
	 */
	public static String getDateTimeStrEn(Date date)
	{
		if (date == null)
			return getCurDateTimeStr(dateFormatEn);
		else
			return dateFormatEn.format(date);
	}

	/**
	 * 将日期date转换为 yyyy年MM月dd日中文格式的字符串
	 * @param date 日期时间变量
	 * @return yyyy年MM月dd日 格式的日期字符串
	 */
	public static String getDateTimeStrCn(Date date)
	{
		if (date == null)
			return getCurDateTimeStr(dateFormatCn);
		else
			return dateFormatCn.format(date);
	}

	/**
	 * 将日期字符串dateStr转换为 dd-MM-yyyy 英文格式的字符串
	 * @param date 日期时间变量
	 * @return dd-MM-yyyy格式的日期字符串
	 */
	public static String getDateTimeStrEn(String dateStr)
		throws Exception
	{
		if (dateStr == null)
			return getCurDateTimeStr(dateFormatEn);
		else
			return getDateTimeStrEn(parseDate(dateStr));
	}

	/**
	 * 将日期字符串dateStr转换为 yyyy年MM月dd日中文格式的字符串
	 * @param date 日期时间变量
	 * @return yyyy年MM月dd日中文 格式的日期字符串
	 */
	public static String getDateTimeStrCn(String dateStr)
		throws Exception
	{
		if (dateStr == null)
			return getCurDateTimeStr(dateFormatCn);
		else
			return getDateTimeStrCn(parseDate(dateStr));
	}

	/**
	 * 将日期date转换为 MM-yyyy 英文格式的字符串
	 * @param date 日期时间变量
	 * @return dd-MM-yyyy格式的日期字符串
	 */
	public static String getDateTimeStrYMEn(Date date)
	{
		if (date == null)
			return getCurDateTimeStr(dateFormatYMEn);
		else
			return dateFormatYMEn.format(date);
	}

	public static String getDateTimeStrYMCn(Date date)
	{
		if (date == null)
			return getCurDateTimeStr(dateFormatYMCn);
		else
			return dateFormatYMCn.format(date);
	}

	public static String getDateTimeStrYMEn(String dateStr)
		throws Exception
	{
		if (dateStr == null)
			return getCurDateTimeStr(dateFormatYMEn);
		else
			return getDateTimeStrYMEn(parseDate(dateStr));
	}

	public static String getDateTimeStrYMCn(String dateStr)
		throws Exception
	{
		if (dateStr == null)
			return getCurDateTimeStr(dateFormatYMCn);
		else
			return getDateTimeStrYMCn(parseDate(dateStr));
	}

	public static String getDateTimeStrMDEn(Date date)
	{
		if (date == null)
			return getCurDateTimeStr(dateFormatMDEn);
		else
			return dateFormatMDEn.format(date);
	}

	public static String getDateTimeStrMDCn(Date date)
	{
		if (date == null)
			return getCurDateTimeStr(dateFormatMDCn);
		else
			return dateFormatMDCn.format(date);
	}

	public static String getDateTimeStrMDEn(String dateStr)
		throws Exception
	{
		if (dateStr == null)
			return getCurDateTimeStr(dateFormatMDEn);
		else
			return getDateTimeStrMDEn(parseDate(dateStr));
	}

	public static String getDateTimeStrMDCn(String dateStr)
		throws Exception
	{
		if (dateStr == null)
			return getCurDateTimeStr(dateFormatMDCn);
		else
			return getDateTimeStrMDCn(parseDate(dateStr));
	}

	private static Calendar clearDate(Date date, int clearNum)
	{
		Calendar cal = new GregorianCalendar();
		cal.setTime(date);
		if (clearNum > 0)
			cal.set(14, 0);
		if (clearNum > 1)
			cal.set(13, 0);
		if (clearNum > 2)
			cal.set(12, 0);
		if (clearNum > 3)
			cal.set(11, 0);
		if (clearNum > 4)
			cal.set(5, 0);
		if (clearNum > 5)
			cal.set(2, 0);
		return cal;
	}

	public static String[] getDateLimitStr()
		throws Exception
	{
		return getDateLimitStr(new Date());
	}

	public static String[] getWeekLimitStr()
		throws Exception
	{
		return getWeekLimitStr(new Date());
	}

	public static String[] getMonthLimitStr()
		throws Exception
	{
		return getMonthLimitStr(new Date());
	}

	public static String[] getYearLimitStr()
		throws Exception
	{
		return getYearLimitStr(new Date());
	}

	public static String[] getDateLimitStr(Date date)
		throws Exception
	{
		Date rtDateArray[] = getDateLimit(date);
		return (new String[] {
			getDateTimeStr(rtDateArray[0]), getDateTimeStr(rtDateArray[1])
		});
	}

	public static String[] getWeekLimitStr(Date date)
		throws Exception
	{
		Date rtDateArray[] = getWeekLimit(date);
		return (new String[] {
			getDateTimeStr(rtDateArray[0]), getDateTimeStr(rtDateArray[1])
		});
	}

	public static String[] getMonthLimitStr(Date date)
		throws Exception
	{
		Date rtDateArray[] = getMonthLimit(date);
		return (new String[] {
			getDateTimeStr(rtDateArray[0]), getDateTimeStr(rtDateArray[1])
		});
	}

	public static String[] getYearLimitStr(Date date)
		throws Exception
	{
		Date rtDateArray[] = getYearLimit(date);
		return (new String[] {
			getDateTimeStr(rtDateArray[0]), getDateTimeStr(rtDateArray[1])
		});
	}

	public static Date[] getDateLimit(Date date)
		throws Exception
	{
		Calendar cal = clearDate(date, 4);
		Date date1 = cal.getTime();
		cal.add(5, 1);
		cal.add(13, -1);
		Date date2 = cal.getTime();
		return (new Date[] {
			date1, date2
		});
	}

	public static Date getFirstDayOfWeek(Date date)
	{
		Calendar c = clearDate(date, 4);
		c.setFirstDayOfWeek(2);
		c.set(7, c.getFirstDayOfWeek());
		return c.getTime();
	}

	public static Date getLastDayOfWeek(Date date)
	{
		Calendar c = clearDate(date, 4);
		c.setFirstDayOfWeek(2);
		c.set(7, c.getFirstDayOfWeek() + 6);
		c.set(11, 23);
		c.set(12, 59);
		c.set(13, 59);
		c.set(14, 0);
		return c.getTime();
	}

	public static Date[] getWeekLimit(Date date)
		throws Exception
	{
		Date date1 = getFirstDayOfWeek(date);
		Date date2 = getLastDayOfWeek(date);
		return (new Date[] {
			date1, date2
		});
	}

	public static Date[] getMonthLimit(Date date)
		throws Exception
	{
		Calendar cal = clearDate(date, 5);
		cal.set(5, 1);
		Date date1 = cal.getTime();
		cal.add(2, 1);
		cal.add(13, -1);
		Date date2 = cal.getTime();
		return (new Date[] {
			date1, date2
		});
	}

	public static Date[] getYearLimit(Date date)
		throws Exception
	{
		Calendar cal = clearDate(date, 6);
		cal.set(2, 0);
		cal.set(5, 1);
		Date date1 = cal.getTime();
		cal.add(1, 1);
		cal.add(13, -1);
		Date date2 = cal.getTime();
		return (new Date[] {
			date1, date2
		});
	}

	public static int getDaySpan(String toDateStr)
		throws Exception
	{
		return (int)((parseDate(toDateStr).getTime() - (new Date()).getTime()) / 86400000);
	}

	public static int getDaySpan(Date toDate)
	{
		return (int)((toDate.getTime() - (new Date()).getTime()) / 86400000);
	}

	public static int getDaySpan(Date fromDate, Date toDate)
	{
		return (int)((toDate.getTime() - fromDate.getTime()) / 86400000);
	}

	
	public static int getTimeSpan(String toDateStr)
	throws Exception
	{
		return (int)((parseDate(toDateStr).getTime() - (new Date()).getTime()) / 1000);
	}

	public static int getTimeSpan(Date toDate)
	{
		return (int)((toDate.getTime() - (new Date()).getTime()) / 1000);
	}

	public static int getTimeSpan(Date fromDate, Date toDate)
	{
		return (int)((toDate.getTime() - fromDate.getTime()) / 1000);
	}

	public static Date getDayBefore(String dateStr, int dayCnt)
		throws Exception
	{
		return getDayBefore(parseDate(dateStr), dayCnt);
	}

	public static Date getDayBefore(Date date, int dayCnt)
	{
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(5, 0 - dayCnt);
		return cal.getTime();
	}

	public static Date getDayAfter(String dateStr, int dayCnt)
		throws Exception
	{
		return getDayAfter(parseDate(dateStr), dayCnt);
	}

	public static Date getDayAfter(Date date, int dayCnt)
	{
		if(date==null)return null;
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(5, dayCnt);
		return cal.getTime();
	}

	public static Date getDayDiff(String dateStr, int dayCnt)
		throws Exception
	{
		return getDayDiff(parseDate(dateStr), dayCnt);
	}

	public static Date getDayDiff(Date date, int dayCnt)
	{
		if(date==null)return null;
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(5, dayCnt);
		return cal.getTime();
	}

	public static Date getYestday(String dateStr)
		throws Exception
	{
		return getYestday(parseDate(dateStr));
	}

	public static Date getYestday(Date date)
	{
		if(date==null)return null;
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(5, -1);
		return cal.getTime();
	}

	public static String getYestdayStr(String dateStr)
		throws Exception
	{
		return getYestdayStr(parseDate(dateStr));
	}

	public static String getYestdayStr(Date date)
	{
		if(date==null)return null;
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(5, -1);
		return getDateTimeStr(cal.getTime());
	}

	public static String getMonthBefore(String dateStr, int diff)
		throws Exception
	{
		return getMonthBefore(parseDate(dateStr), diff);
	}

	public static String getMonthBefore(Date date, int diff)
	{
		if(date==null)return null;
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(2, 0 - diff);
		return getDateTimeStr(cal.getTime());
	}

	public static String getMonthAfter(String dateStr, int diff)
		throws Exception
	{
		return getMonthAfter(parseDate(dateStr), diff);
	}

	public static String getMonthAfter(Date date, int diff)
	{
		if(date==null)return null;
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(2, 0 + diff);
		return getDateTimeStr(cal.getTime());
	}

	public static double getDoubleFromStr(String numStr)
	{
		double rtValue = 0.0D;
		try
		{
			rtValue = numFormatG.parse(numStr).doubleValue();
		}
		catch (Exception exception) { }
		return rtValue;
	}

	public static long getLongFromStr(String numStr)
	{
		long rtValue = 0L;
		try
		{
			rtValue = numFormatG.parse(numStr).longValue();
		}
		catch (Exception exception) { }
		return rtValue;
	}

	public static int getIntFromStr(String numStr)
	{
		int rtValue = 0;
		try
		{
			rtValue = numFormatG.parse(numStr).intValue();
		}
		catch (Exception exception) { }
		return rtValue;
	}

	public static long getFileSize(String fileName)
	{
		long rtSize = 0L;
		File file = new File(fileName);
		rtSize = file.length();
		return rtSize;
	}

	public static String getRequestPath(String uri)
	{
		String rtStr = null;
		int tmpIndex = uri.lastIndexOf("/");
		if (tmpIndex >= 0)
			rtStr = uri.substring(0, tmpIndex);
		return rtStr;
	}

	public static String getFixLengthNum(String str, int length)
	{
		if (str == null)
			str = "";
		String preFix = "";
		for (int i = 0; i < length - str.length(); i++)
			preFix = (new StringBuilder(String.valueOf(preFix))).append("0").toString();

		return (new StringBuilder(String.valueOf(preFix))).append(str).toString();
	}

	public static String get36BaseStr(long num, int length)
		throws Exception
	{
		String numStr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		long tmpArray[] = get10ArrayFromNum(num, 36, new long[0]);
		String tmpStr = "";
		for (int i = 0; i < tmpArray.length; i++)
			tmpStr = (new StringBuilder(String.valueOf(tmpStr))).append(String.valueOf(numStr.charAt((int)tmpArray[i]))).toString();

		String rtStr = getFixLengthNum(tmpStr, length);
		return rtStr;
	}

	public static long get10BaseNum(String srcNum)
		throws Exception
	{
		if (srcNum == null)
			return 0L;
		String numStr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		char charArray[] = srcNum.toCharArray();
		int exponent = 0;
		long num36 = 0L;
		for (int i = charArray.length - 1; i >= 0; i--)
		{
			char tmpChar = charArray[i];
			int tmpNum = numStr.indexOf(tmpChar);
			if (tmpNum >= 0)
				num36 = (long)((double)num36 + (double)tmpNum * Math.pow(36D, exponent++));
			else
				throw new Exception(I18n.ln(MYUtility, "1"));
		}

		return num36;
	}

	public static String getNextNum(String srcNum, int diff, int length)
		throws Exception
	{
		String rtStr = null;
		if (srcNum == null || length <= 0)
			return rtStr;
		String numStr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		char charArray[] = srcNum.toCharArray();
		int exponent = 0;
		long num36 = 0L;
		for (int i = charArray.length - 1; i >= 0; i--)
		{
			char tmpChar = charArray[i];
			int tmpNum = numStr.indexOf(tmpChar);
			if (tmpNum >= 0)
				num36 = (long)((double)num36 + (double)tmpNum * Math.pow(36D, exponent++));
			else
				throw new Exception(I18n.ln(MYUtility, "1"));
		}

		num36 += diff;
		long tmpArray[] = get10ArrayFromNum(num36, 36, new long[0]);
		String tmpStr = "";
		for (int i = 0; i < tmpArray.length; i++)
			tmpStr = (new StringBuilder(String.valueOf(tmpStr))).append(String.valueOf(numStr.charAt((int)tmpArray[i]))).toString();

		rtStr = getFixLengthNum(tmpStr, length);
		return rtStr;
	}

	public static long[] get10ArrayFromNum(long num, int baseNum, long num10[])
		throws Exception
	{
		long tmpArray[] = new long[num10.length + 1];
		tmpArray[0] = num % (long)baseNum;
		if (num10.length > 0)
			System.arraycopy(num10, 0, tmpArray, 1, num10.length);
		if (num / (long)baseNum == 0L)
			return tmpArray;
		else
			return get10ArrayFromNum(num / (long)baseNum, baseNum, tmpArray);
	}

	public static String getFixLengthStringFront(String str, int length)
	{
		String rtStr = str;
		if (rtStr == null)
			return rtStr;
		if (rtStr.length() >= length)
			return rtStr;
		for (int i = rtStr.length(); i < length; i++)
			rtStr = (new StringBuilder("0")).append(rtStr).toString();

		return rtStr;
	}

	public static String getFixLengthStringPost(String str, int length)
	{
		String rtStr = str;
		if (rtStr == null)
			return rtStr;
		if (rtStr.length() >= length)
			return rtStr;
		for (int i = rtStr.length(); i < length; i++)
			rtStr = (new StringBuilder(String.valueOf(rtStr))).append("0").toString();

		return rtStr;
	}

	private static String positiveIntegerToChinsesStr(String numStr)
	{
		String chineseDigiStr[] = {
			"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"
		};
		String chineseDiviStr[] = {
			"", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", 
			"佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", 
			"万", "拾", "佰", "仟"
		};
		String rmbStr = "";
		boolean lastzero = false;
		boolean hasvalue = false;
		int len = numStr.length();
		if (len > 15)
			return I18n.ln(MYUtility, "2");
		for (int i = len - 1; i >= 0; i--)
			if (numStr.charAt(len - i - 1) != ' ')
			{
				int n = numStr.charAt(len - i - 1) - 48;
				if (n < 0 || n > 9)
					return I18n.ln(MYUtility, "3");
				if (n != 0)
				{
					if (lastzero)
						rmbStr = (new StringBuilder(String.valueOf(rmbStr))).append(chineseDigiStr[0]).toString();
					if (n != 1 || i % 4 != 1 || i != len - 1)
						rmbStr = (new StringBuilder(String.valueOf(rmbStr))).append(chineseDigiStr[n]).toString();
					rmbStr = (new StringBuilder(String.valueOf(rmbStr))).append(chineseDiviStr[i]).toString();
					hasvalue = true;
				} else
				if (i % 8 == 0 || i % 8 == 4 && hasvalue)
					rmbStr = (new StringBuilder(String.valueOf(rmbStr))).append(chineseDiviStr[i]).toString();
				if (i % 8 == 0)
					hasvalue = false;
				lastzero = n == 0 && i % 4 != 0;
			}

		if (rmbStr.length() == 0)
			return chineseDigiStr[0];
		else
			return rmbStr;
	}

	public static String numToRMBStr(double val)
	{
		String chineseDigiStr[] = {
			"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"
		};
		String SignStr = "";
		String TailStr = "";
		if (val < 0.0D)
		{
			val = -val;
			SignStr = "负";
		}
		if (val > 100000000000000D || val < -100000000000000D)
			return I18n.ln(MYUtility, "2");
		long temp = Math.round(val * 100D);
		long integer = temp / 100L;
		long fraction = temp % 100L;
		int jiao = (int)fraction / 10;
		int fen = (int)fraction % 10;
		if (jiao == 0 && fen == 0)
		{
			TailStr = I18n.ln(MYUtility, "5");
		} else
		{
			TailStr = chineseDigiStr[jiao];
			if (jiao != 0)
				TailStr = (new StringBuilder(String.valueOf(TailStr))).append(I18n.ln(MYUtility, "6")).toString();
			if (integer == 0L && jiao == 0)
				TailStr = "";
			if (fen != 0)
				TailStr = (new StringBuilder(String.valueOf(TailStr))).append(chineseDigiStr[fen]).append(I18n.ln(MYUtility, "7")).toString();
		}
		return (new StringBuilder(String.valueOf(SignStr))).append(positiveIntegerToChinsesStr(String.valueOf(integer))).append(I18n.ln(MYUtility, "8")).append(TailStr).toString();
	}

	public static String numToRMBStr(double val, String currName)
	{
		String chineseDigiStr[] = {
			"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"
		};
		String SignStr = "";
		String TailStr = "";
		if (val < 0.0D)
		{
			val = -val;
			SignStr = "负";
		}
		if (val > 100000000000000D || val < -100000000000000D)
			return "数值位数过大!";
		long temp = Math.round(val * 100D);
		long integer = temp / 100L;
		long fraction = temp % 100L;
		int jiao = (int)fraction / 10;
		int fen = (int)fraction % 10;
		if (jiao == 0 && fen == 0)
		{
			TailStr = "整";
		} else
		{
			TailStr = chineseDigiStr[jiao];
			if (jiao != 0)
				TailStr = (new StringBuilder(String.valueOf(TailStr))).append("角").toString();
			if (integer == 0L && jiao == 0)
				TailStr = "";
			if (fen != 0)
				TailStr = (new StringBuilder(String.valueOf(TailStr))).append(chineseDigiStr[fen]).append("分").toString();
		}
		return (new StringBuilder(String.valueOf(currName))).append(SignStr).append(positiveIntegerToChinsesStr(String.valueOf(integer))).append("元").append(TailStr).toString();
	}

	public static String number2En(double val)
	{
		String intString;
		String decString;
		String engCapital;
		String moneyString = getFormatedStr(val, 1);
		String tmpString[] = moneyString.split("\\.");
		intString = moneyString;
		decString = "";
		engCapital = "";
		StringBuffer strBuff1 = null;
		StringBuffer strBuff2 = null;
		StringBuffer strBuff3 = null;
		int curPoint = 0;
		int i1 = 0;
		int i2 = 0;
		int i3 = 0;
		int k = 0;
		int n = 0;
		if (tmpString.length > 1)
		{
			intString = tmpString[0];
			decString = tmpString[1];
		}
		try{
			decString = (new StringBuilder(String.valueOf(decString))).append("00").toString();
			decString = decString.substring(0, 2);
			curPoint = intString.length() - 1;
			if (curPoint >= 0 && curPoint < 15)
			{
				k = 0;
				for (; curPoint >= 0; curPoint -= 3)
				{
					strBuff1 = new StringBuffer("");
					strBuff2 = new StringBuffer("");
					strBuff3 = new StringBuffer("");
					if (curPoint >= 2)
					{
						n = Integer.parseInt(intString.substring(curPoint - 2, curPoint + 1));
						if (n != 0)
						{
							i1 = n / 100;
							i2 = (n - i1 * 100) / 10;
							i3 = n - i1 * 100 - i2 * 10;
							if (i1 != 0)
								strBuff1.append((new StringBuilder(String.valueOf(enSmallNumber[i1]))).append(" HUNDRED ").toString());
							if (i2 != 0)
							{
								if (i2 == 1)
								{
									strBuff2.append((new StringBuilder(String.valueOf(enSmallNumber[i2 * 10 + i3]))).append(" ").toString());
								} else
								{
									strBuff2.append((new StringBuilder(String.valueOf(enLargeNumber[i2 - 2]))).append(" ").toString());
									if (i3 != 0)
										strBuff3.append((new StringBuilder(String.valueOf(enSmallNumber[i3]))).append(" ").toString());
								}
							} else
							if (i3 != 0)
								strBuff3.append((new StringBuilder(String.valueOf(enSmallNumber[i3]))).append(" ").toString());
							engCapital = (new StringBuilder(String.valueOf(strBuff1.toString()))).append(strBuff2.toString()).append(strBuff3.toString()).append(enUnit[k]).append(" ").append(engCapital).toString();
						}
					} else
					{
						n = Integer.parseInt(intString.substring(0, curPoint + 1));
						if (n != 0)
						{
							i2 = n / 10;
							i3 = n - i2 * 10;
							if (i2 != 0)
							{
								if (i2 == 1)
								{
									strBuff2.append((new StringBuilder(String.valueOf(enSmallNumber[i2 * 10 + i3]))).append(" ").toString());
								} else
								{
									strBuff2.append((new StringBuilder(String.valueOf(enLargeNumber[i2 - 2]))).append(" ").toString());
									if (i3 != 0)
										strBuff3.append((new StringBuilder(String.valueOf(enSmallNumber[i3]))).append(" ").toString());
								}
							} else
							if (i3 != 0)
								strBuff3.append((new StringBuilder(String.valueOf(enSmallNumber[i3]))).append(" ").toString());
							engCapital = (new StringBuilder(String.valueOf(strBuff2.toString()))).append(strBuff3.toString()).append(enUnit[k]).append(" ").append(engCapital).toString();
						}
					}
					k++;
				}
	
				engCapital = engCapital.trim();
			}
			strBuff2 = new StringBuffer();
			strBuff3 = new StringBuffer();
			n = Integer.parseInt(decString);
			if (n != 0)
			{
				i2 = n / 10;
				i3 = n - i2 * 10;
				if (i2 != 0)
				{
					if (i2 == 1)
					{
						strBuff2.append((new StringBuilder(String.valueOf(enSmallNumber[i2 * 10 + i3]))).append(" ").toString());
					} else
					{
						strBuff2.append((new StringBuilder(String.valueOf(enLargeNumber[i2 - 2]))).append(" ").toString());
						if (i3 != 0)
							strBuff3.append((new StringBuilder(String.valueOf(enSmallNumber[i3]))).append(" ").toString());
					}
				} else
				if (i3 != 0)
					strBuff3.append((new StringBuilder(String.valueOf(enSmallNumber[i3]))).append(" ").toString());
				if (engCapital.length() > 0)
					engCapital = (new StringBuilder(String.valueOf(engCapital))).append(" AND CENTS ").append(strBuff2).append(strBuff3).toString();
				else
					engCapital = (new StringBuilder("CENTS ")).append(strBuff2).append(strBuff3).toString();
			}
			engCapital = engCapital.trim();
			return engCapital;
		}
		catch(Exception ex){
			ex.printStackTrace();
			return "";
		}
	}

	public static boolean isNumber(String str)
	{
		if (str == null || str.equals(""))
			return false;
		else
			return Pattern.matches("-?\\d*\\.?\\d+(E\\d+)?", str);
	}

	public static boolean isAmtNumber(String str)
	{
		if (str == null || str.equals(""))
			return false;
		else
			return Pattern.matches("-?\\d{1,3}(\\,\\d{3})*\\.\\d{0,8}", str);
	}

	public static boolean isInteger(String str)
	{
		if (str == null || str.equals(""))
			return false;
		else
			return Pattern.matches("(-?[1-9]\\d*)|0", str);
	}

	public static boolean isPositiveInteger(String str)
	{
		if (str == null || str.equals(""))
			return false;
		else
			return Pattern.matches("[1-9]\\d*", str);
	}

	public static boolean isAcctYMFormat(String str)
	{
		if (str == null || str.equals(""))
			return false;
		else
			return Pattern.matches("\\d{4}-\\d{2}", str);
	}

	public static boolean isSysDateFormat(String str)
	{
		if (str == null || str.equals(""))
			return false;
		if (str.length() < 10)
			return false;
		if (str.length() == 10)
			return Pattern.matches("\\d{4}-\\d{2}-\\d{2}", str);
		else
			return Pattern.matches("\\d{4}-\\d{2}-\\d{2}\\s{1}\\d{2}:\\d{2}:\\d{2}", str);
	}

	public static boolean isDay(String str)
	{
		if (str == null || str.equals(""))
			return false;
		if (str.length() < 10)
			return false;
		else
			return Pattern.matches("\\d{4}-\\d{2}-\\d{2}", str);
	}

	public static boolean isDayTime(String str)
	{
		if (str == null || str.equals(""))
			return false;
		if (str.length() < 19)
			return false;
		else
			return Pattern.matches("\\d{4}-\\d{2}-\\d{2}\\s{1}\\d{2}:\\d{2}:\\d{2}", str);
	}

	public static boolean isBracketMatched(String expr)
	{
		if (expr == null || expr.equals(""))
			return true;
		int tmpIndex = expr.indexOf('(');
		if (tmpIndex < 0)
			return true;
		int stackInt = 1;
		for (tmpIndex++; tmpIndex < expr.length();)
		{
			char c = expr.charAt(tmpIndex++);
			if (c == '(')
				stackInt++;
			else
			if (c == ')')
				stackInt--;
		}

		return stackInt == 0;
	}

	public static String num2Charactor(int num)
	{
		if (num < 10 || num > 35)
			return String.valueOf(num);
		else
			return String.valueOf((char)(55 + num));
	}

	/**
	 *  将数字字符串转化为Int型
	 * @param charactor 数字字符串
	 * @return　charactor为NULL或者为空时返回0 
	 */
	public static int str2Num(String charactor)
	{
		if (charactor == null || charactor.equals(""))
			return 0;
		int rtInt = 0;
		String tmpStr = charactor.toUpperCase();
		for (int i = 0; i < tmpStr.length(); i++)
		{
			char c = tmpStr.charAt(tmpStr.length() - 1 - i);
			rtInt += ((c - 65) + 1) * (int)Math.pow(26D, i);
		}

		return rtInt - 1;
	}
	/**
	 * 将字符串数据转化为Int型数值
	 * @param str字符串数值
	 * @return Int型数值
	 */
	public static int parseInt(String str)
	{
		return parseInt(str,0);
	}
	/**
	 * 将字符串数据转化为Int型数值
	 * @param str 字符串数值
	 * @param i  如为空或者NULL时的默认值
	 * @return  Int型数值
	 */
	public static int parseInt(String str, int i) {
		if(str==null || str.equals(""))return i;
		try{
			return Integer.parseInt(str);
		}
		catch(Exception ex){return i;}
	}

	
	/**
	 * 将字符串数据转化为BigDecimal型数值
	 * @param str字符串数值
	 * @return  BigDecimal型数值
	 */
	public static BigDecimal parseBigDecimal(String str)
	{
		if(str==null || str.equals(""))return new BigDecimal(0);
		else return new BigDecimal(str);
	}
	
	/**
	 * 将字符串数据转化为BigDecimal型数值
	 * @param str 字符串数值
	 * @param i  如为空或者NULL时的默认值
	 * @return  BigDecimal型数值
	 */
	public static BigDecimal parseBigDecimal(String str, BigDecimal i) {
		if(str==null || str.equals(""))return i;
		try{
			return new BigDecimal(str);
		}
		catch(Exception ex){return i;}
	}
	/**
	 * 将long数据转化为BigDecimal型数值
	 * @param str字符串数值
	 * @return BigDecimal型数值
	 */
	public static BigDecimal parseBigDecimal(long str)
	{
		return new BigDecimal(str);
	}
	
	/**
	 * 将double数据转化为BigDecimal型数值
	 * @param str字符串数值
	 * @return BigDecimal型数值
	 */
	public static BigDecimal parseBigDecimal(double str)
	{
		return new BigDecimal(str);
	}

	/**
	 * 将float数据转化为BigDecimal型数值
	 * @param str字符串数值
	 * @return BigDecimal型数值
	 */
	public static BigDecimal parseBigDecimal(float str)
	{
		return new BigDecimal(str);
	}
	
	/**
	 * 将int数据转化为BigDecimal型数值
	 * @param str字符串数值
	 * @return BigDecimal型数值
	 */
	public static BigDecimal parseBigDecimal(int str)
	{
		return new BigDecimal(str);
	}

	/**
	 * 将日期字符串转化为日期Date型
	 * @param formatStr 转化格式 yyyy-MM-dd,yyyy年MM月dd日,MMMyyyy等
	 * @param dateStr 日期字符串
	 * @return 返回日期date型
	 * @throws ParseException
	 */
	public static Date parseDate(String formatStr, String dateStr)
		throws ParseException
	{
		if(dateStr==null || dateStr.length()<1)return new Date(0);
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		return format.parse(dateStr);
	}

	public static Date parseDate(String dateStr)
		throws ParseException
	{
		if (dateStr == null || "".equals(dateStr))
			return null;
		SimpleDateFormat format = null;
		if (Pattern.matches("\\d{4}-\\d{1,2}-\\d{1,2}", dateStr))
			format = new SimpleDateFormat("yyyy-MM-dd");
		else
		if (Pattern.matches("\\d{4}\\d{2}\\d{2}", dateStr))
			format = new SimpleDateFormat("yyyyMMdd");
		else
		if (Pattern.matches("\\d{4}年\\d{2}月\\d{2}日", dateStr))
			format = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);
		else
		if (Pattern.matches("\\d{4}年\\d{1,2}月\\d{1,2}日", dateStr))
			format = new SimpleDateFormat("yyyy年M月d日", Locale.CHINA);
		else
		if (Pattern.matches("\\d{1,2}\\w{3}\\d{4}", dateStr))
			format = new SimpleDateFormat("dMMMyyyy", Locale.ENGLISH);
		else
		if (Pattern.matches("\\d{1,2}-\\w{3}-\\d{4}", dateStr))
			format = new SimpleDateFormat("d-MMM-yyyy", Locale.ENGLISH);
		else
		if (dateStr.length() > 20)
			format = dateFormatS;
		else
			format = dateFormat;
		return format.parse(dateStr);
	}
	@SuppressWarnings("deprecation")
	public static Date toDate(String dateStr)
	throws ParseException
	{
		if (dateStr == null || "".equals(dateStr) || dateStr.indexOf("0000-00-00")>=0)
			return null;
		int y=0,m=0,d=0,h=0,n=0,s=0;
    	int pos;

    	pos=dateStr.indexOf("-");
    	if(pos<0){
    		pos=dateStr.indexOf(":");
    		if(pos<0)return null;
    		
    		h=Integer.parseInt(dateStr.substring(0,pos));
	    	dateStr=dateStr.substring(pos+1);
    		pos=dateStr.indexOf(":");
    		if(pos<0){
    			n=Integer.parseInt(dateStr);
    			s=0;
    		}
    		else{
    			n=Integer.parseInt(dateStr.substring(0,pos));
    			dateStr=dateStr.substring(pos+1);
    			pos=dateStr.indexOf(".");
    			if(pos<0)s=Integer.parseInt(dateStr);
    			else s=Integer.parseInt(dateStr.substring(0,pos));
    		}
    	}
    	else {
	    	y=Integer.parseInt(dateStr.substring(0,pos));
	
	    	dateStr=dateStr.substring(pos+1);
	    	pos=dateStr.indexOf("-");
	    	m=Integer.parseInt(dateStr.substring(0,pos));
	
	    	dateStr=dateStr.substring(pos+1);
	    	pos=dateStr.indexOf(" ");
	    	if(pos<0){
	    		d=Integer.parseInt(dateStr);
	    		h=0;
	    		n=0;
	    		s=0;
	    	}
	    	else{
	    		d=Integer.parseInt(dateStr.substring(0,pos));
		    	dateStr=dateStr.substring(pos+1);
	    		pos=dateStr.indexOf(":");
	    		h=Integer.parseInt(dateStr.substring(0,pos));
		    	dateStr=dateStr.substring(pos+1);
	    		pos=dateStr.indexOf(":");
	    		if(pos<0){
	    			n=Integer.parseInt(dateStr);
	    			s=0;
	    		}
	    		else{
	    			n=Integer.parseInt(dateStr.substring(0,pos));
	    			dateStr=dateStr.substring(pos+1);
	    			pos=dateStr.indexOf(".");
	    			if(pos<0)s=Integer.parseInt(dateStr);
	    			else s=Integer.parseInt(dateStr.substring(0,pos));
	    		}
	    	}
    	}
    	return new Date(y-1900,m-1,d,h,n,s);
	}

	public static Timestamp parseTimeStamp()
		throws ParseException
	{
		return parseTimeStamp(((String) (null)));
	}

	public static Timestamp parseTimeStamp(long longDate)
		throws ParseException
	{
		Date utilDate = new Date(longDate);
		Timestamp sqlDate = new Timestamp(utilDate.getTime());
		return sqlDate;
	}

	public static Timestamp parseTimeStamp(String dateStr)
		throws ParseException
	{
		Date utilDate = null;
		if (dateStr == null)
			utilDate = new Date();
		else
			utilDate = parseDate(dateStr);
		Timestamp sqlDate = new Timestamp(utilDate.getTime());
		return sqlDate;
	}

	public static java.sql.Date parseSqlDate()
		throws ParseException
	{
		return parseSqlDate("");
	}

	public static java.sql.Date parseSqlDate(String dateStr)
		throws ParseException
	{
		Date utilDate = null;
		if (dateStr == null || dateStr.length()<1)
			utilDate = new Date();
		else
			utilDate = parseDate(dateStr);
		java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
		return sqlDate;
	}

	public static java.sql.Date parseSqlDate(Date date)
	throws ParseException
	{
		if (date == null)return null;
		java.sql.Date sqlDate = new java.sql.Date(date.getTime());
		return sqlDate;
	}
	public static double parseDouble(String numStr)
		throws ParseException
	{
		if (isNullorEmpty(numStr))
			return 0.0D;
		if (numStr.indexOf(",") > 0)
			return numFormatG.parse(numStr).doubleValue();
		else
			return Double.parseDouble(numStr);
	}

	public static String transferCode(String srcStr, String srcCode, String desCode)
		throws UnsupportedEncodingException
	{
		if (srcStr == null)
			return null;
		else
			return new String(srcStr.getBytes(srcCode), desCode);
	}

	public static String iso88591ToGbk(String srcStr)
		throws UnsupportedEncodingException
	{
		return transferCode(srcStr, "ISO-8859-1", "GBK");
	}

	public static String iso88591ToUTF8(String srcStr)
		throws UnsupportedEncodingException
	{
		return transferCode(srcStr, "ISO-8859-1", "UTF-8");
	}

	public static String getHtmlIdent(int identCnt)
	{
		return getIdentStr(identCnt, "&nbsp;");
	}

	public static String getChinaSpaceIdent(int identCnt)
	{
		return getIdentStr(identCnt, "　");
	}

	public static boolean isEqualAmt(double amt1, double amt2, int amtScale)
	{
		if (amt1 == amt2)
			return true;
		return Math.abs(amt1 - amt2) < Math.pow(10D, 0 - amtScale);
	}

	private static String getIdentStr(int identCnt, String identStr)
	{
		StringBuffer buff = new StringBuffer();
		for (int i = 0; i < identCnt; i++)
			buff.append(identStr);

		return buff.toString();
	}

	public static String loadStream(InputStream in)
		throws Exception
	{
		ByteArrayOutputStream out = null;
		try
		{
			out = new ByteArrayOutputStream();
			byte buff[] = new byte[1024];
			for (int readLen = 0; (readLen = in.read(buff)) > 0;)
				out.write(buff, 0, readLen);

		}
		catch (Exception ex)
		{
			log.debug(ex.getMessage(), ex);
			throw ex;
		}
		try
		{
			if (out != null)
				out.close();
		}
		catch (Exception exception1) { }
		try
		{
			if (out != null)
				out.close();
		}
		catch (Exception exception2) { }
		return out.toString();
	}

	public static String exCmd(String cmd)
	{
		String outMsrg;
		try{
			Runtime r = Runtime.getRuntime();
			Process process = r.exec(cmd);
			InputStream in = null;
			in = process.getInputStream();
			outMsrg = loadStream(in);
			process.waitFor();
			process.destroy();
			return outMsrg;
		}
		catch(Exception ex){
			log.debug(ex.getMessage(), ex);
			return null;
		}
	}

	public static String decimal2DoubleStr(BigDecimal bgd)
	{
		return getFormatedStr(bgd.doubleValue(), 1);
	}

	public static BigDecimal add(double opt1, double opt2, int scale)
	{
		BigDecimal bgd1 = new BigDecimal(opt1);
		BigDecimal bgd2 = new BigDecimal(opt2);
		return add(bgd1, bgd2, scale);
	}

	public static BigDecimal add(BigDecimal bgd1, double opt2, int scale)
	{
		BigDecimal bgd2 = new BigDecimal(opt2);
		return add(bgd1, bgd2, scale);
	}

	public static BigDecimal add(BigDecimal opt1, BigDecimal opt2, int scale)
	{
		if (opt1 == null)
			opt1 = new BigDecimal(0.0D);
		if (opt2 == null)
			opt2 = new BigDecimal(0.0D);
		opt1 = opt1.setScale(2,scale);
		opt2 = opt2.setScale(2,scale);
		return opt1.add(opt2);
	}

	public static BigDecimal subtract(double opt1, double opt2, int scale)
	{
		BigDecimal bgd1 = new BigDecimal(opt1);
		BigDecimal bgd2 = new BigDecimal(opt2);
		return subtract(bgd1, bgd2, scale);
	}

	public static BigDecimal subtract(BigDecimal bgd1, double opt2, int scale)
	{
		BigDecimal bgd2 = new BigDecimal(opt2);
		return subtract(bgd1, bgd2, scale);
	}

	public static BigDecimal subtract(BigDecimal opt1, BigDecimal opt2, int scale)
	{
		if (opt1 == null)
			opt1 = new BigDecimal(0.0D);
		if (opt2 == null)
			opt2 = new BigDecimal(0.0D);
		opt1 = opt1.setScale(2,scale);
		opt2 = opt2.setScale(2,scale);
		return opt1.subtract(opt2);
	}

	public static BigDecimal multiply(double opt1, double opt2, int scale)
	{
		BigDecimal bgd1 = new BigDecimal(opt1);
		BigDecimal bgd2 = new BigDecimal(opt2);
		return multiply(bgd1, bgd2, scale);
	}

	public static BigDecimal multiply(BigDecimal bgd1, double opt2, int scale)
	{
		BigDecimal bgd2 = new BigDecimal(opt2);
		return multiply(bgd1, bgd2, scale);
	}

	public static BigDecimal multiply(BigDecimal opt1, BigDecimal opt2, int scale)
	{
		if (opt1 == null)
			opt1 = new BigDecimal(0.0D);
		if (opt2 == null)
			opt2 = new BigDecimal(0.0D);
		opt1 = opt1.setScale(2,scale);
		opt2 = opt2.setScale(2,scale);
		return opt1.multiply(opt2);
	}

	public static BigDecimal divide(double opt1, double opt2, int scale)
	{
		BigDecimal bgd1 = new BigDecimal(opt1);
		BigDecimal bgd2 = new BigDecimal(opt2);
		return divide(bgd1, bgd2, scale);
	}

	public static BigDecimal divide(BigDecimal bgd1, double opt2, int scale)
	{
		BigDecimal bgd2 = new BigDecimal(opt2);
		bgd2.setScale(2,scale);
		return divide(bgd1, bgd2, scale);
	}

	public static BigDecimal divide(BigDecimal opt1, BigDecimal opt2, int scale)
	{
		if (opt1 == null)
			opt1 = new BigDecimal(0.0D);
		if (opt2 == null)
			opt2 = new BigDecimal(1.0D);
		opt1 = opt1.setScale(2,scale);
		opt2 = opt2.setScale(2,scale);
		return opt1.divide(opt2, scale * 2, 4);
	}

	public static String parseDoubleStr(BigDecimal bg, int format)
	{
		return getFormatedStr(bg.doubleValue(), format);
	}

	public static double parseDouble(BigDecimal bg)
	{
		if (bg == null)
			return 0.0D;
		else
			return Double.parseDouble(parseDoubleStr(bg, 1));
	}

	public static String getRandomName()
	{
		String currTimeStr = getCurDateTimeStr(DATE_FORMAT_NOSPLIT);
		String randomStr = String.valueOf((int)Math.random() * 10000);
		return (new StringBuilder(String.valueOf(currTimeStr))).append(randomStr).toString();
	}

	public static void str2Map(String propStr, Map<String, Object> rtMap)
	{
		String propArray[] = propStr.split("\\\r*\\\n{1}");
		for (int i = 0; i < propArray.length; i++)
		{
			String tmpStr = propArray[i].trim();
			if (tmpStr.length() >= 1 && !tmpStr.startsWith("#"))
			{
				int tmpIndex = tmpStr.indexOf("=");
				if (tmpIndex >= 0)
				{
					String key = tmpStr.substring(0, tmpIndex).trim();
					String value = tmpStr.substring(tmpIndex + 1).trim();
					if (key.length() >= 1 && value.length() >= 1)
						rtMap.put(key, value);
				}
			}
		}

	}

	public static void copyMap(Map<String, Object> fromMap, Map<String, Object> toMap)
	{
		Object key;
		Object value;
		for (Iterator<?> iKeys = fromMap.keySet().iterator(); iKeys.hasNext(); toMap.put((String) key, value))
		{
			key = iKeys.next();
			value = fromMap.get(key);
		}

	}

	public static void clearMapPre(Map<?, ?> srcMap, String preFix)
	{
		List<String> keyList = new ArrayList<String>();
		for (Iterator<?> iKeys = srcMap.keySet().iterator(); iKeys.hasNext();)
		{
			String key = (String)iKeys.next();
			if (key.startsWith(preFix))
				keyList.add(key);
		}

		for (int i = 0; i < keyList.size(); i++)
			srcMap.remove(keyList.get(i));

	}

	public static void clearMapPost(Map<?, ?> srcMap, String postFix)
	{
		List<String> keyList = new ArrayList<String>();
		for (Iterator<?> iKeys = srcMap.keySet().iterator(); iKeys.hasNext();)
		{
			String key = (String)iKeys.next();
			if (key.endsWith(postFix))
				keyList.add(key);
		}

		for (int i = 0; i < keyList.size(); i++)
			srcMap.remove(keyList.get(i));

	}

	public static String encodeURL(String srcStr)
		throws Exception
	{
		if (srcStr == null)
			return "";
		else
			return URLEncoder.encode(srcStr, Const.DEFAULT_CODE);
	}

	public static String decodeURL(String srcStr)
		throws Exception
	{
		if (srcStr == null)
			return "";
		else
			return URLDecoder.decode(srcStr, Const.DEFAULT_CODE);
	}

	public static String encodeHtml(String srcStr)
	{
		if (srcStr == null)
			return "";
		else
			return srcStr.replace("\\", "&#92;").replace("\"", "&#34;").replace("'", "&#39;").replace("\r\n", "&#10;&#13;").replace("\n", "&#13;").replace("\r", "&#10;");
	}

	public static String encodeQuote(String srcStr)
	{
		if (srcStr == null)
			return "";
		else
			return srcStr.replace("\\", "\\\\").replace("\"", "\\\"").replace("'", "\\'");
	}

	public static String encodeSpecial(String srcStr)
	{
		if (srcStr == null)
			return "";
		else
			return srcStr.replace("\\", "\\\\").replace("\"", "\\\"").replace("'", "\\'").replace("\r\n", "").replace("\n", "").replace("\r", "");
	}

	public static String encodeLike(String srcStr)
	{
		if (srcStr == null)
			return "";
		else
			return srcStr.replace("\\", "\\\\").replace("'", "''").replace("%", "\\%").replace("_", "\\_");
	}

	public static Map<String, Object> startsWithMap(Map<?, ?> srcMap, String prefix)
	{
		Map<String, Object> rtMap = new HashMap<String, Object>();
		if (isNullorEmpty(prefix))
			return rtMap;
		Iterator<?> iKeys = srcMap.keySet().iterator();
		while (iKeys.hasNext())
		{
			String key = (String)iKeys.next();
			if (key.startsWith(prefix))
				rtMap.put(key, srcMap.get(key));
		}

		return rtMap;
	}

	public static Map<String, ?> endsWithMap(Map<?, ?> srcMap, String postfix)
	{
		Map<String, Object> rtMap = new HashMap<String, Object>();
		if (isNullorEmpty(postfix))
			return rtMap;
		for (Iterator<?> iKeys = srcMap.keySet().iterator(); iKeys.hasNext();)
		{
			String key = (String)iKeys.next();
			if (key.endsWith(postfix))
				rtMap.put(key, srcMap.get(key));
		}

		return rtMap;
	}

//	public static void sortDesc(List list, String key, int type)
//	{
//		Collections.sort(list, new MYMapComparator(type, key, "DESC"));
//	}
//
//	public static void sortAsc(List list, String key, int type)
//	{
//		Collections.sort(list, new MYMapComparator(type, key, "ASC"));
//	}

	public static String cutText(String text)
	{
		String result="";
		result = text.replaceAll("[\\s*\t\n\r]", "");
		return result;
	}
	public static String cutHtml(String html)
	{
		String result = "";
		result = html.replaceAll("(<[^/\\s][\\w]*)[\\s]*([^>]*)(>)", "$1$3").replaceAll("<[^>]*>", "");
		return result;
	}

//	public static Long cast2Long(Object value)
//		throws MYInvalidParamException
//	{
//		if (value == null || value.toString().equals("")) return new Long(0L);
//		try{
//			return (Long)value;
//		}
//		catch(Exception ex){
//			try{
//				return new Long(((Integer)value).intValue());
//			}
//			catch(Exception ex2){
//				try{
//					int valueInt = (int)((Double)value).doubleValue();
//					return Long.valueOf(valueInt);
//				}
//				catch(Exception ex3){
//					throw new MYInvalidParamException((new StringBuilder("不能转换成长整型 >> ")).append(value.toString()).append(" >> ").append(value.getClass().getName()).toString());
//				}
//			}
//		}
//	}
	static 
	{
		dateFormatEn = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
		dateFormatCn = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINESE);
		dateFormatYMEn = new SimpleDateFormat("MMM-yyyy", Locale.ENGLISH);
		dateFormatYMCn = new SimpleDateFormat("yyyy年MM月", Locale.CHINESE);
		dateFormatMDEn = new SimpleDateFormat("dd-MMM", Locale.ENGLISH);
		dateFormatMDCn = new SimpleDateFormat("MM-dd", Locale.CHINESE);
	}
    /**
     *  把一个以逗号分隔的字符串转化为一个数组。
     */
    public static String[] StringToStringArray(String attribName){
		int paraNum=0;
		String tempStr=attribName;
		while(tempStr.indexOf(",")!=-1){
			tempStr=tempStr.substring(tempStr.indexOf(",")+1);
			paraNum++;
		}
		paraNum++;
		String[] S2SA=new String[paraNum];
		int i=0;
		int pos=0;
		while(attribName.indexOf(",")!=-1){
			pos=attribName.indexOf(",");
			S2SA[i]=attribName.substring(0,pos);
			attribName=attribName.substring(pos+1);
			i++;
		}
		S2SA[i]=attribName;
		return S2SA;
    }

    public static String[] StringToStringArray(String attribName,String compartChar){
		int paraNum=0;
		String tempStr=attribName;
		while(tempStr.indexOf(compartChar)!=-1){
			tempStr=tempStr.substring(tempStr.indexOf(compartChar)+compartChar.length());
			paraNum++;
		}
		paraNum++;
		String[] S2SA=new String[paraNum];
		int i=0;
		int pos=0;
		while(attribName.indexOf(compartChar)!=-1){
			pos=attribName.indexOf(compartChar);
			S2SA[i]=attribName.substring(0,pos);
			attribName=attribName.substring(pos+compartChar.length());
			i++;
		}
		S2SA[i]=attribName;
		return S2SA;
    }

/**
 *  把一个以逗号分隔的字符串转化为一个数组。
 */
    public static String[] StringToStringArray(String attribName,int startPos){
		int paraNum=0;
		String tempStr=attribName;
		while(tempStr.indexOf(",")!=-1){
			tempStr=tempStr.substring(tempStr.indexOf(",")+1);
			paraNum++;
		}
		paraNum++;
    	if(startPos>paraNum){
    		startPos=0;
    	}
		String[] S2SA=new String[paraNum];
		int i=0;
		int j=0;
		int pos=0;
		while(attribName.indexOf(",")!=-1){
			pos=attribName.indexOf(",");
			if(j>=startPos){
				S2SA[i]=attribName.substring(0,pos);
				i++;
			}
			attribName=attribName.substring(pos+1);
			j++;
		}
		S2SA[i]=attribName;
		return S2SA;
    }

/**
 *  把一个以逗号分隔的字符串转化为一个数组。
 */
    public static String[] StringToStringArray(String attribName,String compartChar,int startPos){
		int paraNum=0;
		String tempStr=attribName;
		while(tempStr.indexOf(compartChar)!=-1){
			tempStr=tempStr.substring(tempStr.indexOf(compartChar)+1);
			paraNum++;
		}
		paraNum++;
    	if(startPos>paraNum){
    		startPos=0;
    	}
		String[] S2SA=new String[paraNum];
		int i=0;
		int j=0;
		int pos=0;
		while(attribName.indexOf(compartChar)!=-1){
			pos=attribName.indexOf(compartChar);
			if(j>=startPos){
				S2SA[i]=attribName.substring(0,pos);
				i++;
			}
			attribName=attribName.substring(pos+1);
			j++;
		}
		S2SA[i]=attribName;
		return S2SA;
    }

/**
 *  把一个以逗号分隔的字符串转化为一个数组。
 */
    public static String[] StringToStringArray(String attribName,int startPos,int endPos){
		int paraNum=0;
		String tempStr=attribName;
		while(tempStr.indexOf(",")!=-1){
			tempStr=tempStr.substring(tempStr.indexOf(",")+1);
			paraNum++;
		}
		paraNum++;
    	if(startPos>paraNum){
    		startPos=0;
    	}
		String[] S2SA=new String[paraNum];
		int i=0;
		int j=0;
		int pos=0;
		while((attribName.indexOf(",")!=-1)&&j<endPos){
			pos=attribName.indexOf(",");
			if(j>=startPos){
				S2SA[i]=attribName.substring(0,pos);
				i++;
			}
			attribName=attribName.substring(pos+1);
			j++;
		}
		if(attribName.indexOf(",")!=-1){
			S2SA[i]=attribName.substring(0,attribName.indexOf(","));
		}
		else{
			S2SA[i]=attribName;
		}
		return S2SA;
    }

/**
 *  把一个以逗号分隔的字符串转化为一个数组。
 */
    public static String[] StringToStringArray(String attribName,String compartChar,int startPos,int endPos){
		int paraNum=0;
		String tempStr=attribName;
		while(tempStr.indexOf(compartChar)!=-1){
			tempStr=tempStr.substring(tempStr.indexOf(compartChar)+1);
			paraNum++;
		}
		paraNum++;
    	if(startPos>paraNum){
    		startPos=0;
    	}
		String[] S2SA=new String[paraNum];
		int i=0;
		int j=0;
		int pos=0;
		while((attribName.indexOf(compartChar)!=-1)&&j<endPos){
			pos=attribName.indexOf(compartChar);
			if(j>=startPos){
				S2SA[i]=attribName.substring(0,pos);
				i++;
			}
			attribName=attribName.substring(pos+1);
			j++;
		}
		if(attribName.indexOf(compartChar)!=-1){
			S2SA[i]=attribName.substring(0,attribName.indexOf(compartChar));
		}
		else{
			S2SA[i]=attribName;
		}
		return S2SA;
    }

/**
 *  把一个数组转化为一个以逗号分隔的字符串。
 */
    public static String StringArrayToString(String[] attribName){
    	String SA2S=attribName[0];
		for(int i=1;i<(attribName.length);i++){
			SA2S=SA2S+","+attribName[i];
		}
		return SA2S;
    }

/**
 *  把一个数组转化为一个以逗号分隔的字符串。
 */
    public static String StringArrayToString(String[] attribName,String compartChar){
    	String SA2S=attribName[0];
		for(int i=1;i<(attribName.length);i++){
			SA2S=SA2S+compartChar+attribName[i];
		}
		return SA2S;
    }

/**
 *  把一个数组转化为一个以逗号分隔的字符串。
 */
    public static String StringArrayToString(String[] attribName,int startPos){
    	if(startPos>attribName.length){
    		startPos=0;
    	}
    	String SA2S=attribName[startPos];
		for(int i=startPos+1;i<(attribName.length);i++){
			SA2S=SA2S+","+attribName[i];
		}
		return SA2S;
    }

/**
 *  把一个数组转化为一个以逗号分隔的字符串。
 */
    public static String StringArrayToString(String[] attribName,String compartChar,int startPos){
    	if(startPos>attribName.length){
    		startPos=0;
    	}
    	String SA2S=attribName[startPos];
		for(int i=startPos+1;i<(attribName.length);i++){
			SA2S=SA2S+compartChar+attribName[i];
		}
		return SA2S;
    }

/**
 *  把一个数组转化为一个以逗号分隔的字符串。
 */
    public static String StringArrayToString(String[] attribName,int startPos,int endPos){
    	if(startPos>attribName.length){
    		startPos=0;
    	}
    	if(startPos>attribName.length){
    		startPos=attribName.length;
    	}
    	String SA2S=attribName[startPos];
		for(int i=startPos+1;i<endPos;i++){
			SA2S=SA2S+","+attribName[i];
		}
		return SA2S;
    }

/**
 *  把一个数组转化为一个以逗号分隔的字符串。
 */
    public static String StringArrayToString(String[] attribName,String compartChar,int startPos,int endPos){
    	if(startPos>attribName.length){
    		startPos=0;
    	}
    	if(startPos>attribName.length){
    		startPos=attribName.length;
    	}
    	String SA2S=attribName[startPos];
		for(int i=startPos+1;i<endPos;i++){
			SA2S=SA2S+compartChar+attribName[i];
		}
		return SA2S;
    }
    
    public static String getContextPath(HttpServletRequest request)
    {
		String contextPath = request.getContextPath();
		if (isNullorEmpty(contextPath))contextPath = "/e1";
		return contextPath;
    }
    public final static String MD5(String s) {
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};       
        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
	public static String zeroToEmpty(int num) {
		if(num==0)return "";
		return String.valueOf(num);
	}
	public static String zeroToEmpty(int num,String defaultValue) {
		if(num==0)return defaultValue;
		return String.valueOf(num);
	}
}
